
/******************************/
/** ENTREPRENEUR - REPAYMENT **/
/******************************/



double focR(const double xval, void * params) // JUST A FUNCTION, DOES NOT CONSTITUTE A VALUE FUNCTION
{
    struct paramsgoldenRB *focparams= (struct paramsgoldenRB *) params;
    
    double *expE_WWNE_JNE = focparams->expE_WWNE_JNE_ST;
    double *expE_UnNE_JNE = focparams->expE_UnNE_JNE_ST;
#if OPT_compute_C_flow == 1
    double *C_expE_WWNE_JNE = focparams->C_expE_WWNE_JNE_ST;
    double *C_expE_UnNE_JNE = focparams->C_expE_UnNE_JNE_ST;
#endif
#if SEA == 1
    double *expE_WWNE_JNEi = focparams->expE_WWNE_JNEi_ST;
    double *expE_UiNE_JNEi = focparams->expE_UiNE_JNEi_ST;
#if OPT_compute_C_flow == 1
    double *C_expE_WWNE_JNEi = focparams->C_expE_WWNE_JNEi_ST;
    double *C_expE_UiNE_JNEi = focparams->C_expE_UiNE_JNEi_ST;
#endif
#endif
    
    int igridCOHfoc=focparams->igridCOHST; int tgridfoc=focparams->tgridST; int zgridfoc=focparams->zgridST;
    int Jinsured=focparams->JinsuredST;
    
    double cons, xgrid, dxgrid, valfoc, searcheffW;
    double tempWWNE_JNE, tempUiNE_JNEi, tempUnNE_JNE, tempWWNE_JNEi, C_tempWWNE_JNE, C_tempUiNE_JNEi, C_tempUnNE_JNE, C_tempWWNE_JNEi;
    double diffValue, diffgrid, ddiffgrid;
    int iverif, ixgrid,  inxE,inxE1, idiffgrid;
    
    
    // CONSUMPTION //
    cons = findconsRB(xval, gridCOH[igridCOHfoc]);
    
    
    // ON THE GRID REDEFINITION //
    xgrid = invexpspace(xval,Gridmin,Gridmax,Echelle1,maxgrid);
    dxgrid = weightinter(xval, xgrid, &ixgrid, grid, maxgrid);
    if ((xval>(Gridmax))){printf("focR: (xval>(Gridmax)) %20.15f\n",xval);getchar();}
    if ((ixgrid>=(maxgrid-1))){printf("focR: ixgrid>=(maxgrid-1) %d\n",ixgrid);getchar();}
    if ((cons<=0.0)){printf("focR: consoFOC<0.0 %20.15f\t%20.15f\t%20.15f\n",xval,cons, findconsRB(0.0, gridCOH[igridCOHfoc]));getchar();}
    if ((xval<(0.0))){printf("focR_1: (xval<(0.0)) %20.15f\n",xval);getchar();}
    
    
    // CONTINUATION VALUE //
    inxE  = inxE(ixgrid,tgridfoc,zgridfoc);
    inxE1 = inxE((ixgrid+1),tgridfoc,zgridfoc);
    
    tempWWNE_JNE = inter1d(dxgrid,expE_WWNE_JNE[inxE],expE_WWNE_JNE[inxE1]);
    tempUnNE_JNE = inter1d(dxgrid,expE_UnNE_JNE[inxE],expE_UnNE_JNE[inxE1]);
#if OPT_compute_C_flow == 1
    C_tempWWNE_JNE = inter1d(dxgrid,C_expE_WWNE_JNE[inxE],C_expE_WWNE_JNE[inxE1]);
    C_tempUnNE_JNE = inter1d(dxgrid,C_expE_UnNE_JNE[inxE],C_expE_UnNE_JNE[inxE1]);
#endif
#if SEA == 1
    if(Jinsured == 1){
        tempUiNE_JNEi = inter1d(dxgrid,expE_UiNE_JNEi[inxE],expE_UiNE_JNEi[inxE1]);
        tempWWNE_JNEi = inter1d(dxgrid,expE_WWNE_JNEi[inxE],expE_WWNE_JNEi[inxE1]);
#if OPT_compute_C_flow == 1
        C_tempUiNE_JNEi = inter1d(dxgrid,C_expE_UiNE_JNEi[inxE],C_expE_UiNE_JNEi[inxE1]);
        C_tempWWNE_JNEi = inter1d(dxgrid,C_expE_WWNE_JNEi[inxE],C_expE_WWNE_JNEi[inxE1]);
#endif
    }
#endif
    
    
    // FIND THE OPTIMAL EFFORT LEVEL
    if(Jinsured == 0){diffValue = max(0.000000000001,(betapar*(tempWWNE_JNE - tempUnNE_JNE)));}
#if SEA == 1
    if(Jinsured == 1) { // correspond to insured next period
        diffValue = max(0.000000000001,(betapar*(tempWWNE_JNEi - tempUiNE_JNEi)));
    }
#endif
    diffgrid   = invexpspace(diffValue,gridminsearch,gridmaxsearch,EchelleW,maxsearch);
    ddiffgrid  = weightinter(diffValue, diffgrid, &idiffgrid, diffvalues, maxsearch);
    searcheffW = inter1d(ddiffgrid,search1D_W[inxS(idiffgrid, tgridfoc)],search1D_W[inxS(idiffgrid+1, tgridfoc)]);
    focparams->searchWoutST=searcheffW;
    
    
    // COMPUTE THE CONTINUATION VALUE
    if(Jinsured == 0){
        valfoc = utilc(cons,1) + disutilityW(searcheffW) + betapar*(piW(searcheffW)*tempWWNE_JNE + (1.0-piW(searcheffW))*tempUnNE_JNE);
#if OPT_compute_C_flow == 1
        focparams->C_val = utilc(cons,1) + betapar*(piW(searcheffW)*C_tempWWNE_JNE + (1.0-piW(searcheffW))*C_tempUnNE_JNE);
#endif
    }
#if SEA == 1
    if(Jinsured == 1) {
        valfoc = utilc(cons,1) + disutilityW(searcheffW) + betapar*(piW(searcheffW)*tempWWNE_JNEi + (1.0-piW(searcheffW))*tempUiNE_JNEi);
#if OPT_compute_C_flow == 1
        focparams->C_val = utilc(cons,1) + betapar*(piW(searcheffW)*C_tempWWNE_JNEi + (1.0-piW(searcheffW))*C_tempUiNE_JNEi);
#endif
    }
#endif
    
    valfoc=-valfoc;
    return valfoc;
}
